# CMakeLists.txt for timeflow_engine (Linux platform)
#
# This file configures the build system for a C project that supports
# multiple AI inference backends (e.g., native CPU implementation and MindSpore Lite).
# It demonstrates how to:
#   - Set minimum CMake version and project properties
#   - Include header search paths
#   - Collect source files conditionally
#   - Link against static third-party libraries
#   - Prepare for future extensibility (e.g., optional backends)

cmake_minimum_required(VERSION 3.10)

# Declare the project name and primary language.
# Specifying "C" ensures CMake uses the C compiler (e.g., gcc) and C-specific defaults.
project(timeflow_engine C)

# Enforce C99 standard for all compiled C files.
# This ensures consistent behavior across compilers and enables modern C features.
set(CMAKE_C_STANDARD 99)

# ============================================================================
# Header include directories
# ----------------------------------------------------------------------------
# These paths are added to the compiler's include search list (-I flags).
# Use relative paths from the project root for portability.
# ============================================================================
include_directories(
    # Core public headers of the engine (e.g., htf_engine.h)
    ../../include

    # Third-party library headers (e.g., flatbuffers, jpeglib.h)
    ${CMAKE_SOURCE_DIR}/third_party/include

    # Glue code that wraps third-party APIs for use in C
    ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty_glue

    # Utility modules (e.g., logging, memory helpers)
    ${CMAKE_SOURCE_DIR}/utils

    # Current directory (for local headers, if any)
    .
)

# ============================================================================
# Library search paths
# ----------------------------------------------------------------------------
# Tells the linker where to find static (.a) or shared (.so) libraries.
# This is necessary when linking against pre-built third-party static libraries.
# ============================================================================
link_directories(
    ${CMAKE_SOURCE_DIR}/third_party/lib
)

# ============================================================================
# Source file list
# ----------------------------------------------------------------------------
# Explicitly list all C source files to be compiled into the executable.
# Avoid using file(GLOB ...) in production builds—it hides dependency changes from CMake.
#
# Note:
#   - The MindSpore backend (htf_ms_backend.c) is included by default here,
#     but in a more advanced setup, you might conditionally include it
#     using CMake options (e.g., -DENABLE_MINDSPORE=ON).
#   - Protobuf glue is commented out because Protobuf is a C++ library,
#     and mixing C and C++ requires careful handling (extern "C", etc.).
#     Since this is a pure C project, Protobuf is not directly usable.
# ============================================================================
set(SOURCES
    # Core engine logic
    ../../src/htf_engine.c

    # Backend factory: selects the appropriate backend at runtime
    backend/htf_ai_backend_factory.c

    # Native (CPU) backend implementation
    backend/native/htf_native_backend.c
    backend/native/htf_native_model_parser.c
    backend/native/htf_native_infer_core.c

    # MindSpore Lite backend (C API wrapper)
    backend/mindspore/htf_ms_backend.c

    # Third-party integration layers
    thirdparty_glue/htf_flatbuffers_glue.c
    # thirdparty_glue/htf_protobuf_glue.c  # Disabled: Protobuf is C++; not compatible with pure C

    # Utility wrappers for common formats
    thirdparty_glue/htf_jpeg_decoder.c   # Uses libjpeg
    thirdparty_glue/htf_zlib_wrapper.c   # Uses zlib
)

# ============================================================================
# Define the executable target
# ----------------------------------------------------------------------------
# CMake will compile all listed source files and link them into a single binary.
# The name of the executable is "timeflow_engine".
# ============================================================================
add_executable(timeflow_engine ${SOURCES})

# ============================================================================
# Link dependencies
# ----------------------------------------------------------------------------
# Specify libraries to link against.
# - Libraries prefixed with "lib" and ending in ".a" are static libraries.
#   CMake automatically maps "libflatbuffers.a" → -lflatbuffers if found in link_directories.
# - System libraries (pthread, dl, m) are linked by name without "lib" prefix.
#
# Important notes:
#   - libjpeg.a and libturbojpeg.a are mutually exclusive in practice.
#     Standard libjpeg (jpeglib.h) ≠ libturbojpeg (turbojpeg.h).
#     Ensure your source code matches the library you link.
#   - libsecurec.a is a non-standard, Huawei-specific secure C library.
#     On standard Linux, consider replacing securec functions with standard C equivalents.
#   - libcrypto.a and libssl.a are only needed if your code uses OpenSSL (e.g., HTTPS, encryption).
#     Remove them if unused to reduce binary size and dependencies.
# ============================================================================
target_link_libraries(timeflow_engine
    # Third-party static libraries (assumed to exist in third_party/lib/)
    libflatbuffers.a    # FlatBuffers serialization (C headers)
    libjpeg.a           # Standard IJG libjpeg (for jpeglib.h)
    libturbojpeg.a      #  Only if using turbojpeg.h! Otherwise, remove this.
    libz.a              # zlib compression/decompression
    libsecurec.a        #  Non-standard; consider removal for portability
    libcrypto.a         #  Only if using OpenSSL crypto functions
    libssl.a            #  Only if using OpenSSL SSL/TLS

    # Essential system libraries
    pthread             # POSIX threads
    dl                  # Dynamic linking (dlopen, etc.)
    m                   # Math library (sin, cos, log, etc.)
)

# ============================================================================
# Future improvement suggestions:
# ----------------------------------------------------------------------------
# 1. Use CMake options to enable/disable backends:
#      option(ENABLE_MINDSPORE "Build with MindSpore Lite support" OFF)
#      if(ENABLE_MINDSPORE)
#          list(APPEND SOURCES backend/mindspore/htf_ms_backend.c)
#          target_link_libraries(timeflow_engine mindspore-lite)
#      endif()
#
# 2. Replace static library names with imported targets or find_package() for better portability.
#
# 3. Validate library existence at configure time to avoid linker errors.
#
# 4. Consider using add_library() for reusable components instead of a monolithic executable.
# ============================================================================